package com.briup.jdbc;

import org.junit.Test;

import java.sql.*;

/**
 * 对边批处理和预编译，效率上的提升
 * Statement
 * PreparedStatement
 * 同时插入10000条数据进入数据库
 */
public class Test7 {
    public static final String driver= "com.mysql.cj.jdbc.Driver";
    public static final String url= "jdbc:mysql:///db01?useServerPrepStmts=true";
    public static final String username= "root";
    public static final String password= "rootroot";
    @Test
    public void stInsert(){
        Connection connection = null;
        Statement statement = null;
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url,username,password);
            // 创建语句对象
            statement = connection.createStatement();
            long start = System.currentTimeMillis();
            // 准备sql语句
            for(int i=3;i<=10003;i++){
                String sql = "insert into t_user values("+i+",'tom"+i+"','123456',20)";
                statement.execute(sql);
            }
            long end = System.currentTimeMillis();
            System.out.println("插入成功，共耗时："+(end-start)+"毫秒");

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {

            if(statement != null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if(connection != null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    @Test
    public void pstInsert(){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url,username,password);
            // 创建语句对象
            String sql = "insert into t_user values(?,?,?,?)";
            preparedStatement = connection.prepareStatement(sql);
            long start = System.currentTimeMillis();
            // 准备sql语句
            for(int i=3;i<=100003;i++){
                // 传入数据，并加入到batch中
               preparedStatement.setInt(1,i);
               preparedStatement.setString(2,"tom"+i);
               preparedStatement.setString(3,"123456");
               preparedStatement.setInt(4,20);
               preparedStatement.addBatch();
               if(i % 5000 == 0){
                   preparedStatement.executeBatch();
                   preparedStatement.clearBatch();
               }
            }
            preparedStatement.executeBatch();

            long end = System.currentTimeMillis();
            System.out.println("插入成功，共耗时："+(end-start)+"毫秒");

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {

            if(preparedStatement != null){
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if(connection != null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
